/* ******************************************************************
 * This file is part of the escal project and distributed under the
 * terms of the GNU General Public License Version 3
 * Copyright (C) 2008 Epocher (xijiaosdq@gmail.com)
 * See COPYING file for licensing information.
 *******************************************************************/

#include "escal.h"
#include "task_info_widget.h"
#include "task.h"
#include <QTabWidget>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextEdit>

using namespace Escal;

TaskInfoWidget::TaskInfoWidget(QWidget *parent)
	:QTabWidget(parent)
{
	setTabPosition(QTabWidget::South);
	watchTask = NULL;
	/* addTab(new QWidget, "tab1");
	addTab(new QWidget, "tab2");
	textDocument = new QTextDocument;
	textCursor = new QTextCursor(textDocument);
	textCursor->beginEditBlock();
	textCursor->insertHtml("<table bgcolor=#4794DC border=1 ><tr><td><b>Hello</b></td><td>World</td></tr>");
	textCursor->endEditBlock();
	textEdit = new QTextEdit;
	textEdit->setAcceptRichText(true);
	textEdit->setDocument(textDocument);
	addTab(textEdit, "tab3");
	*/
};

void TaskInfoWidget::watch(BaseTask *task){
	if(watchTask){
		disconnect(watchTask, 0, this, 0);
		watchTask=NULL;
	}
	watchTask = task;
	qDebug() << task;
	connect(watchTask, SIGNAL(taskDataChanged(BaseTask*, int)),
		this, SLOT(taskDataChanged(BaseTask*, int)));
	QList <QTextDocument *> textDocumentList = task->getTextDocumentList();
	if(textDocumentList.size() <= 0){
		qDebug() << "textDocumentList.size() <= 0";
		return ;
	}
	clear();
	int i;
	while(textEditList.size() < textDocumentList.size()){
		QTextEdit *textEdit = new QTextEdit;
		textEdit->setReadOnly(true);
		textEditList << textEdit;
	}
	while(textEditList.size() > textDocumentList.size()){
		QTextEdit *textEdit = textEditList.last();
		textEditList.removeLast();
		delete textEdit;
	}
	for(i=0;i<textDocumentList.size();i++){
		textEditList[i]->setDocument(textDocumentList[i]);
	}
	addTab(textEditList[0], tr("Task Info"));
	addTab(textEditList[1], tr("File Info"));
	for(i=2;i<textEditList.size();i++){
		addTab(textEditList[i], tr("Link Info"));
	}
};

void TaskInfoWidget::taskDataChanged(BaseTask *task, int column){
	if(task!=watchTask){
		qDebug() << "receive signal from wrong task !";
		return ;
	}
	if(column==UN_RESOURCE){
		int i;
		clear();
		QList <QTextDocument *> textDocumentList = task->getTextDocumentList();
		while(textEditList.size() < textDocumentList.size()){
			QTextEdit *textEdit = new QTextEdit;
			textEdit->setReadOnly(true);
			textEditList << textEdit;
		}
		while(textEditList.size() > textDocumentList.size()){
			QTextEdit *textEdit = textEditList.last();
			textEditList.removeLast();
			delete textEdit;
		}
		for(i=0;i<textDocumentList.size();i++){
			textEditList[i]->setDocument(textDocumentList[i]);
		}
		addTab(textEditList[0], tr("Task Info"));
		addTab(textEditList[1], tr("File Info"));
		for(i=2;i<textEditList.size();i++){
			addTab(textEditList[i], tr("Link Info"));
		}
	}
}

void TaskInfoWidget::taskDeleted(BaseTask *task){
	if(watchTask == task){
		clear();
		int i;
		for(i=0;i<textEditList.size();i++){
			delete textEditList[i];
			textEditList[i]=NULL;
		}
		textEditList.removeAll(NULL);
		watchTask=NULL;
	}
}

